﻿using System;
using System.Drawing;
using ProjectAutumn.Graphics;
using Color = ProjectAutumn.Graphics.Color;

namespace ProjectAutumn.GraphicsDevice.Form
{
    class FormTexture : Texture
    {
        internal Image Internal;
        public FormTexture(int width, int height)
        {
            Internal = new Bitmap(width, height);
        }
        public FormTexture(string file)
        {
            Internal = Image.FromFile(file);
        }
        public override void SetData(Color[] color, int start, int length)
        {
            if (start != 0 && color.Length != length) throw new Exception();
            //TODO: Make it work with a custom start and/or length
            if (Internal is Bitmap)
            {
                Bitmap bitmap = Internal as Bitmap;
                for (int x = 0; x < Width; x++)
                    for (int y = 0; y < Height; y++)
                        bitmap.SetPixel(x, y, ConvertColor(color[y * Width + x]));
            }
            else
            {
                System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(Internal);
                for (int x = 0; x < Width; x++)
                    for (int y = 0; y < Height; y++)
                        graphics.FillRectangle(new SolidBrush(ConvertColor(color[y * Width + x])),
                                               new System.Drawing.Rectangle(x, y, 1, 1));
                graphics.Dispose();
            }
        }
        public override int Width
        {
            get { return Internal.Width; }
        }
        public override int Height
        {
            get { return Internal.Height; }
        }
        public override void Dispose()
        {
            Internal.Dispose();
        }
    }
}
